package com.gouaixin.bee.beeframework2.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.adapter.mFragmentPagerAdapter;
import com.gouaixin.bee.beeframework2.fragment.B5_baobeiZhuanquFragment;
import com.gouaixin.bee.beeframework2.fragment.B5_kankanBierenFragment;
import com.gouaixin.bee.beeframework2.fragment.B5_wodeBaobeiFragment;

import java.util.ArrayList;


/**
 * Created by Administrator on 2015/3/9.
 */

//每日挖宝
public class B5_wabaoActivity extends FragmentActivity {
    Context context = B5_wabaoActivity.this;
    //顶部
    ImageView back;//返回
    TextView title;//标题
    TextView address;//收货地址

    //内容
    public static ViewPager viewpager;
    private mFragmentPagerAdapter adapter;
    private int currentPageIndex = 0;
    private ArrayList<Fragment> datas;

    LinearLayout[] tabBtArray;//tab按钮
    TextView[] tabTvArray;//tab文本
    TextView[] tabBgArray;//tab背景


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b5_wabao);

        initHead();
        initTab();
        initViewPager();



    }

    /**
     * *
     * ViewPager的设置和监听
     */
    private void initViewPager() {
        viewpager = (ViewPager) findViewById(R.id.viewPager);
        adapter = new mFragmentPagerAdapter(this.getSupportFragmentManager(), null);
        datas = new ArrayList<Fragment>();

        datas.add(new B5_baobeiZhuanquFragment());
        datas.add(new B5_wodeBaobeiFragment());
        datas.add(new B5_kankanBierenFragment());

        adapter.setDatas(datas);
        adapter.notifyDataSetChanged();

        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(0);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int index) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int pageIndex) {

                currentPageIndex = pageIndex;

                //根据页面数设置Tab背景
                setTabColor();

            }
        });

    }

    /**
     * *
     * Tab按钮的设置和监听
     */
    private void initTab() {

        tabBtArray = new LinearLayout[]{
                (LinearLayout) this.findViewById(R.id.tab01Bt),
                (LinearLayout) this.findViewById(R.id.tab02Bt),
                (LinearLayout) this.findViewById(R.id.tab03Bt),
        };

        tabTvArray = new TextView[]{
                (TextView) this.findViewById(R.id.tab01Tv),
                (TextView) this.findViewById(R.id.tab02Tv),
                (TextView) this.findViewById(R.id.tab03Tv),
        };

        tabBgArray = new TextView[]{
                (TextView) this.findViewById(R.id.tab01Bg),
                (TextView) this.findViewById(R.id.tab02Bg),
                (TextView) this.findViewById(R.id.tab03Bg),
        };

        for (LinearLayout bt : tabBtArray) {
            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        switch (v.getId()) {
                            case R.id.tab01Bt:
                                currentPageIndex = 0;
                                break;
                            case R.id.tab02Bt:
                                currentPageIndex = 1;
                                break;
                            case R.id.tab03Bt:
                                currentPageIndex =2;
                                break;
                        }
                        setTabColor();
//                        viewpager.setCurrentItem(currentPageIndex);
                        android.util.Log.e("Demo", "顶部Tab:currentPageIndex=" + currentPageIndex);
                    } catch (Exception e) {
                        e.printStackTrace();
                        android.util.Log.e("Demo", "顶部Tab:出问题了=" + e.getMessage());
                    }
                }
            });
        }

    }


    /**
     * 根据currentPageIndex设置Tab按钮的背景
     */
    private void setTabColor() {
        for (int i = 0; i < tabBgArray.length; i++) {
            if (i == currentPageIndex) {
                tabBgArray[i].setVisibility(View.VISIBLE);
                tabTvArray[i].setTextColor(0xffFD6B8F);
            } else {
                tabBgArray[i].setVisibility(View.INVISIBLE);
                tabTvArray[i].setTextColor(0xff7F7F7F);
            }

        }
    }


    /**
     * 顶部的监听
     */
    private void initHead() {
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        address= (TextView) findViewById(R.id.address);
        address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"收货地址"+"功能正在开发中",Toast.LENGTH_SHORT).show();
            }
        });




    }


}



